package com.jackson.utils;

import com.jackson.dao.IUserDao;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

/*
 * @ClassName SpringTest
 * @Author JacksonCode
 * @Version V1.0
 */
public class SpringTest {

    @Test
    public void test1(){
        // 获取到 spring 上下文对象，借助上下文对象可以获取到IOC容器中的额bean对象， 加载的同时就创建了  bean对象到容器中
        ApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        // ApplicationContext fileSystemXmlApplicationContext = new FileSystemXmlApplicationContext("D:\\spring_code\\spring_quickstart\\src\\main\\resources\\applicationContext.xml");

        // 使用上下文对象从IOC容器中获取到了bean对象
        // 1.根据bean id在容器中找对应的bean对象
        //IUserDao userDao = (IUserDao) classPathXmlApplicationContext.getBean("userDao");

        // 2.根据类型在容器中进行查询，有可能出现报错的情况，更具当前类型匹配到多个实例
        IUserDao userDao = classPathXmlApplicationContext.getBean("userDao",IUserDao.class);

        // 调用方法
        userDao.save();

    }

    @Test
    public void test2(){
        // 核心接口, 不会创建bean对象存到容器中
        BeanFactory xmlBeanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));

        // getBean的时候才是真正创建Bean对象
        IUserDao userDao = (IUserDao) xmlBeanFactory.getBean("userDao");
        userDao.save();
    }

    /*
    *  测试scope 属性， singleton效果
    * */
    @Test
    public void test3(){
        ApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        IUserDao userDao = (IUserDao) classPathXmlApplicationContext.getBean("userDao");
        IUserDao userDao2 = (IUserDao) classPathXmlApplicationContext.getBean("userDao");

        System.out.println(userDao);
        System.out.println(userDao2);


    }

    /*
    *  测试 scope属性 prototype效果
    * */
    @Test
    public void test4(){


        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        IUserDao userDao = (IUserDao) classPathXmlApplicationContext.getBean("userDao");

        IUserDao userDao2 = (IUserDao) classPathXmlApplicationContext.getBean("userDao");

        System.out.println(userDao);

        System.out.println(userDao2);

        classPathXmlApplicationContext.close();

    }

}
